package com.egao.common.manage.school;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.component.minio.config.MinioBucket;
import com.egao.common.component.minio.service.MinioService;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.system.constants.UserConstants;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.service.UserService;
import com.egao.common.module.world.entity.WorldGame;
import com.egao.common.module.world.entity.WorldGameStudent;
import com.egao.common.module.world.service.WorldGameService;
import com.egao.common.module.world.service.WorldGameStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 学校济世杯管理
 *
 * @author root
 */
@Api(tags = "学校济世杯管理")
@RestController
@RequestMapping("${api}")
public class SchoolWorldGameController extends BaseController {

    @Autowired
    private WorldGameService worldGameService;
    @Autowired
    private WorldGameStudentService worldGameStudentService;
    @Autowired
    private UserService userService;
    @Autowired
    private MinioService minioService;

    private final String TITLE = "校级济世杯";

    @PreAuthorize("hasAuthority('school:world-game:list')")
    @ApiOperation(value = "分页列表")
    @GetMapping("/school/world-game/page")
    public PageResult<WorldGame> page(HttpServletRequest request) {
        PageParam<WorldGame> pageParam = new PageParam<>(request);
        return new PageResult<>(worldGameService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:world-game:list')")
    @ApiOperation(value = "查看济世杯")
    @GetMapping("/school/world-game/detail")
    public JsonResult detail(Integer id) {
        return JsonResult.ok().setData(worldGameService.getById(id));
    }

    @PreAuthorize("hasAuthority('school:world-game:save')")
    @ApiOperation(value = "添加济世杯")
    @OperLog(value = TITLE, desc = "添加济世杯", result = true)
    @PostMapping("/school/world-game/insert")
    public JsonResult insert(@RequestBody WorldGame worldGame) {
        return worldGameService.insertWorldGame(worldGame, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('school:world-game:update')")
    @ApiOperation(value = "修改济世杯")
    @OperLog(value = TITLE, desc = "修改济世杯", result = true)
    @PutMapping("/school/world-game/update")
    public JsonResult update(@RequestBody WorldGame worldGame) {
        return worldGameService.updateWorldGame(worldGame);
    }

    @PreAuthorize("hasAuthority('school:world-game:delete')")
    @ApiOperation(value = "删除济世杯")
    @OperLog(value = TITLE, desc = "删除济世杯", result = true)
    @PutMapping("/school/world-game/delete")
    public JsonResult delete(@RequestBody WorldGame worldGame) {
        return worldGameService.deleteWorldGame(worldGame);
    }

    @PreAuthorize("hasAuthority('school:world-game:update')")
    @ApiOperation(value = "修改获奖信息")
    @OperLog(value = TITLE, desc = "修改获奖信息", result = true)
    @PutMapping("/school/world-game/prize")
    public JsonResult prize(@RequestBody WorldGameStudent worldGameStudent) {
        worldGameStudent.setPrizeBy(getLoginUserId());
        return worldGameStudentService.updatePrize(worldGameStudent);
    }

    @PreAuthorize("hasAuthority('school:world-game:update')")
    @ApiOperation(value = "修改比赛状态")
    @OperLog(value = TITLE, desc = "修改比赛状态", result = true)
    @PutMapping("/school/world-game/gameState")
    public JsonResult gameState(@RequestBody WorldGameStudent worldGameStudent) {
        worldGameStudent.setPrizeBy(getLoginUserId());
        return worldGameStudentService.updateGameState(worldGameStudent);
    }

    @PreAuthorize("hasAuthority('school:world-game:list')")
    @ApiOperation(value = "学生报名分页列表")
    @GetMapping("/school/world-game/student")
    public PageResult<WorldGameStudent> student(HttpServletRequest request) {
        PageParam<WorldGameStudent> pageParam = new PageParam<>(request);
        List<WorldGameStudent> list = worldGameStudentService.listPage(pageParam);

        for (WorldGameStudent item : list) {
            String fileUrl = minioService.getPreSignedObjectUrl(MinioBucket.MINIO_WORLD, item.getFileUrl());
            item.setFileUrl(fileUrl);
        }

        return new PageResult<>(list, pageParam.getTotal());

    }

    @PreAuthorize("hasAuthority('school:world-game:list')")
    @OperLog(value = TITLE, desc = "导入比赛状态", result = true)
    @ApiOperation("导入比赛状态")
    @PostMapping("/school/world-game/import-game-state")
    public JsonResult importGameState(MultipartFile file, Integer id) {
        return worldGameStudentService.importStudentGameState(file, id);
    }

    @PreAuthorize("hasAuthority('school:world-game:list')")
    @OperLog(value = TITLE, desc = "导入获奖状态", result = true)
    @ApiOperation("导入获奖状态")
    @PostMapping("/school/world-game/import-prize-level")
    public JsonResult importPrizeLevel(MultipartFile file, Integer id) {
        return worldGameStudentService.importStudentPrizeLevel(file, id, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('school:world-game:list')")
    @OperLog(value = "用户管理", desc = "查询全部")
    @ApiOperation("查询全部用户")
    @GetMapping("/school/world-game/user")
    public JsonResult user() {
        List<User> list = userService.list(new LambdaQueryWrapper<User>()
                .eq(User::getUserType, UserConstants.USER_TYPE_TEACHER)
        );
        return JsonResult.ok().setData(list);
    }
}
